package jdbc;

import java.sql.*;
import java.util.Scanner;

public class HomeWordDemo1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入密码:");
        String password = scan.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入年龄:");
        int age = Integer.parseInt(scan.nextLine());
        try (Connection conn = DBUtil.getConnection()){
            String sql = "INSERT INTO user(username,password,nickname,age) "+
                         "VALUES (?,?,?,?) ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setInt(4,age);
            int num = ps.executeUpdate();
            System.out.println(num>0?"注册成功":"注册失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
